/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.admin.entity.MaintainCheckRecord;
import org.springblade.modules.admin.entity.MaintainMalfunctionHistory;
import org.springblade.modules.admin.entity.MaintainPlanContent;
import org.springblade.modules.admin.entity.PlanCheckHistoryScore;
import org.springblade.modules.admin.enums.MaintainPlanContentEnum;
import org.springblade.modules.admin.mapper.MaintainMalfunctionHistoryMapper;
import org.springblade.modules.admin.service.IMaintainMalfunctionHistoryService;
import org.springblade.modules.admin.service.IMaintainPlanContentService;
import org.springblade.modules.admin.service.IPlanCheckHistoryScoreService;
import org.springblade.modules.admin.service.MaintainCheckRecordService;
import org.springblade.modules.admin.vo.MalFunctionHistoryVO;
import org.springblade.modules.admin.vo.plan.store.WxStoreAddQuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 故障工单明细 服务实现类
 *
 * @author zcc
 * @since 2021-05-21
 */
@Service
public class MaintainMalfunctionHistoryServiceImpl extends BaseServiceImpl<MaintainMalfunctionHistoryMapper, MaintainMalfunctionHistory> implements IMaintainMalfunctionHistoryService {


	@Autowired
	@Lazy
	private IPlanCheckHistoryScoreService planCheckHistoryScoreService;

	@Autowired
	@Lazy
	private MaintainCheckRecordService maintainCheckRecordService;

	@Autowired
	@Lazy
	private IMaintainPlanContentService maintainPlanContentService;

	@Override
	public List<MalFunctionHistoryVO> selectByPlanId(Long planId) {
		return this.selectByPlanIds(Arrays.asList(planId));
	}

	@Override
	public List<MalFunctionHistoryVO> selectByPlanIds(List<Long> planIds) {
		return baseMapper.selectByPlanIds(planIds);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveWxQuestion(WxStoreAddQuestionVo wxStoreAddQuestionVo) {
		PlanCheckHistoryScore checkHistoryScore = planCheckHistoryScoreService.getOne(Wrappers.<PlanCheckHistoryScore>lambdaQuery().eq(PlanCheckHistoryScore::getCheckHistoryId, wxStoreAddQuestionVo.getScoreWorkId()));
		if (checkHistoryScore == null) {
			checkHistoryScore = new PlanCheckHistoryScore();
			checkHistoryScore.setScoreDesc(wxStoreAddQuestionVo.getScoreDesc());
			checkHistoryScore.setPlanId(wxStoreAddQuestionVo.getPlanId());
			checkHistoryScore.setCheckHistoryId(wxStoreAddQuestionVo.getScoreWorkId());
			checkHistoryScore.setWxFaultCauseIds(wxStoreAddQuestionVo.getWxFaultCauseIds());
			checkHistoryScore.setWxFaultCauseDesc(wxStoreAddQuestionVo.getWxFaultCauseDesc());
			checkHistoryScore.setWxFaultTypeIds(wxStoreAddQuestionVo.getWxFaultTypeIds());
			checkHistoryScore.setWxFaultTypeDesc(wxStoreAddQuestionVo.getWxFaultTypeDesc());
			planCheckHistoryScoreService.save(checkHistoryScore);
		} else {
			checkHistoryScore.setWxFaultCauseIds(wxStoreAddQuestionVo.getWxFaultCauseIds());
			checkHistoryScore.setWxFaultCauseDesc(wxStoreAddQuestionVo.getWxFaultCauseDesc());
			checkHistoryScore.setWxFaultTypeIds(wxStoreAddQuestionVo.getWxFaultTypeIds());
			checkHistoryScore.setWxFaultTypeDesc(wxStoreAddQuestionVo.getWxFaultTypeDesc());
			checkHistoryScore.setScoreDesc(wxStoreAddQuestionVo.getScoreDesc());
			planCheckHistoryScoreService.updateById(checkHistoryScore);
		}
		List<MaintainMalfunctionHistory> maintainMalfunctionHistorys = wxStoreAddQuestionVo.getMaintainMalfunctionHistorys();


		//表示缓存信息
		Integer wxCache = 2;
		MaintainPlanContent maintainPlanContent = maintainPlanContentService.getById(wxStoreAddQuestionVo.getPlanId());
		if (maintainPlanContent.getTypeStatus().equals(MaintainPlanContentEnum.MAINTAIN_BY.getId())) {
			wxCache = 1;
		}
		if (maintainMalfunctionHistorys != null && maintainMalfunctionHistorys.size() > 0) {
			Integer finalWxCache = wxCache;
			maintainMalfunctionHistorys.stream().forEach(o -> {
				o.setPlanId(wxStoreAddQuestionVo.getPlanId());
				o.setScoreWorkId(wxStoreAddQuestionVo.getScoreWorkId());
				o.setStatus(1);
			});
			this.saveOrUpdateBatch(maintainMalfunctionHistorys);
			for (MaintainMalfunctionHistory maintainMalfunctionHistory : maintainMalfunctionHistorys) {
				MaintainCheckRecord maintainCheckRecord = new MaintainCheckRecord();
				maintainCheckRecord.setCheckHistoryId(maintainMalfunctionHistory.getId());
				maintainCheckRecord.setUserId(AuthUtil.getUserId());
				if (maintainMalfunctionHistory.getId() == null) {
					maintainCheckRecord.setRecordLog(AuthUtil.getNickName() + "-" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + "-创建");
				} else {
					maintainCheckRecord.setRecordLog(AuthUtil.getNickName() + "-" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + "-修改");
				}
				maintainCheckRecordService.save(maintainCheckRecord);
			}
		}
		return true;
	}

	@Override
	public Boolean saveByQuestion(WxStoreAddQuestionVo wxStoreAddQuestionVo) {
		List<MaintainMalfunctionHistory> maintainMalfunctionHistorys = wxStoreAddQuestionVo.getMaintainMalfunctionHistorys();
		if (maintainMalfunctionHistorys != null && maintainMalfunctionHistorys.size() > 0) {
			maintainMalfunctionHistorys.stream().forEach(o -> {
				o.setPlanId(wxStoreAddQuestionVo.getPlanId());
				o.setScoreWorkId(wxStoreAddQuestionVo.getScoreWorkId());
			});
			this.saveOrUpdateBatch(maintainMalfunctionHistorys);
			for (MaintainMalfunctionHistory maintainMalfunctionHistory : maintainMalfunctionHistorys) {
				MaintainCheckRecord maintainCheckRecord = new MaintainCheckRecord();
				maintainCheckRecord.setCheckHistoryId(maintainMalfunctionHistory.getId());
				maintainCheckRecord.setUserId(AuthUtil.getUserId());
				if (maintainMalfunctionHistory.getId() == null) {
					maintainCheckRecord.setRecordLog(AuthUtil.getNickName() + "-" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + "-创建");
				} else {
					maintainCheckRecord.setRecordLog(AuthUtil.getNickName() + "-" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + "-修改");
				}
				maintainCheckRecordService.save(maintainCheckRecord);
			}
		}
		return true;
	}
}

